package com.example.activity_04;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * 单选按钮和复选按钮
 */
public class MainActivity extends Activity {
    private RadioGroup genderGroup;
    private RadioButton femaleButton;
    private RadioButton maleButton;
    private CheckBox swim;
    private CheckBox run;
    private CheckBox read;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        genderGroup=findViewById(R.id.genderGroup);
        femaleButton=findViewById(R.id.femaleButton);
        maleButton=findViewById(R.id.maleButton);
        swim=findViewById(R.id.swim);
        run=findViewById(R.id.run);
        read=findViewById(R.id.read);
        //单选按钮监听器
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(femaleButton.getId()==checkedId){
                    System.out.println("female");
                    //临时出现的文字
                    Toast.makeText(MainActivity.this,"female",Toast.LENGTH_SHORT).show();
                }
                else if(maleButton.getId()==checkedId){
                    System.out.println("male");
                    Toast.makeText(MainActivity.this,"male",Toast.LENGTH_SHORT).show();
                }
            }
        });
        //复选按钮监听器
        swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    System.out.println("swim is checked");
                }
                else{
                    System.out.println("swim is unchecked");
                }
            }
        });
       run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    System.out.println("swim is checked");
                }
                else{
                    System.out.println("swim is unchecked");
                }
            }
        });
        read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    System.out.println("swim is checked");
                }
                else{
                    System.out.println("swim is unchecked");
                }
            }
        });

    }
}
